package com.msa.msaportal.webservice;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.msa.msaportal.common.web.ConstantUntil;
import com.msa.msaportal.entity.PortalExample;
import com.msa.msaportal.entity.PortalPage;
import com.msa.msaportal.entity.PortalRole;
import com.msa.msaportal.entity.PortletEntity;
import com.msa.msaportal.service.IPortalExampleService;
import com.msa.msaportal.service.IPortalPageService;
import com.msa.msaportal.service.IPortalRoleService;

public class PortalResourceServicesImpl implements PortalResourceServices {
	
	@Resource(name="portalManager")
	private IPortalExampleService portalExampleService;
	@Resource(name="roleManager")
	private IPortalRoleService roleManager;
	@Resource(name="pageManager")
	private IPortalPageService pageManager;

	@Override
	public String getPortalByOrgId(String jsonString) {
		
		try {
			//解析json数据，并处理
			JSONObject fromObject = JSONObject.fromObject(jsonString);
			String orgId = fromObject.getString("orgId");
	
			//通过orgid查询
			List<PortalExample> finByOrgId = portalExampleService.getPortalByOrgId(orgId);
			//遍历
			JSONObject jsonn = new JSONObject();
			JSONArray jarry = new JSONArray();
			for (int i = 0; i < finByOrgId.size(); i++) {
				JSONObject jsonp = new JSONObject();
				jsonp.put("portal_id", finByOrgId.get(i).getPortal_id());
				jsonp.put("portal_code", finByOrgId.get(i).getPortal_code());
				jsonp.put("portal_name", finByOrgId.get(i).getPortal_name());
				jsonp.put("orgid", finByOrgId.get(i).getOrgid());
				jsonp.put("orgname", finByOrgId.get(i).getOrgname());
				jsonp.put("createuserid", finByOrgId.get(i).getCreateuserid());
				jsonp.put("createusername", finByOrgId.get(i).getCreateusername());
				jsonp.put("createtime", finByOrgId.get(i).getCreatetime());
				jsonp.put("updateuserid", finByOrgId.get(i).getUpdateuserid());
				jsonp.put("updateusername", finByOrgId.get(i).getUpdateusername());
				jsonp.put("updatetime", finByOrgId.get(i).getUpdatetime());
				jsonp.put("expose", finByOrgId.get(i).isExpose());
				jsonp.put("portal_opinion", finByOrgId.get(i).getPortal_opinion());
				jsonp.put("status", finByOrgId.get(i).isStatus());
				jsonp.put("use_on", finByOrgId.get(i).isUse_on());
				jarry.add(jsonp);
			}
			//添加
			jsonn.put("result", 0);
			jsonn.put("info", "查询成功");
			jsonn.put("data", jarry);
			
			return jsonn.toString(); 
		} catch (Exception e) {
			e.printStackTrace();
			return "{'result':'2','info':'接口异常!'}";
		}		
	}
	
	@Override
	public String portalOpen(String jsonString) {
		
		try {
			//解析json数据，并处理
			JSONObject fromObject = JSONObject.fromObject(jsonString);
			String portalId = fromObject.getString("portalId");
			String portalOpinion = fromObject.getString("portalOpinion");
				//查询数量已发布的数量
			List<PortalExample> number = portalExampleService.getPortalNumber();
			if(number.size()>0){
				return "{'result':'1','info':'您需要先停用已经发布的门户实例'}";
			}else{
				//通过portalId查询
				PortalExample portal = portalExampleService.findByPortalid(portalId);
				portal.setPortal_opinion(portalOpinion);
				portal.setUse_on(true);
				//修改
				portalExampleService.updatePortal(portal);
				//设置发布后所有角色都可见
				List<PortalRole> rolelist = roleManager.getRolelist();
				for(int i=0;i<rolelist.size();i++){
					PortalRole role = rolelist.get(i);
					Set<PortalExample> portalset = new HashSet<PortalExample>();
					portalset.add(portal);
					
					role.setPortalExample(portalset);
					Set<PortalPage> pageset = new HashSet<PortalPage>();
					Set<PortletEntity> portletset = new HashSet<PortletEntity>();
					List<PortalPage> pagelist = pageManager.getPageListByPortal(portal);
					for(int j=0;j<pagelist.size();j++){
						PortalPage page = pagelist.get(j);
						pageset.add(page);						
						if(page.getPageCode().equals(ConstantUntil.HOMEPAGECODE)){
							List<PortletEntity> portletlist = pageManager.getPortletlistBypageId(page.getPageId());
							for(int k=0;k<portletlist.size();k++){
								portletset.add(portletlist.get(k));
							}
						}
						
					}
					role.setPortalPage(pageset);
					role.setPortletEntity(portletset);
					roleManager.updateRole(role);
				}
			}

		} catch (Exception e) {			
			e.printStackTrace();
			return "{'result':'2','info':'接口异常!'}";
		}
		return "{'result':'0','info':'门户实例发布成功!'}";
	}

	@Override
	public String portalClose(String jsonString) {
		//解析json数据，并处理
		JSONObject fromObject = JSONObject.fromObject(jsonString);
		try {
			String portalId = fromObject.getString("portalId");
//			String portalOpinion = fromObject.getString("portalOpinion");
			try {
				//通过portalId查询
				PortalExample portal = portalExampleService.findByPortalid(portalId);
//				portal.setPortal_opinion(portalOpinion);
				portal.setUse_on(false);
				//修改
				portalExampleService.updatePortal(portal);
			} catch (Exception e) {
				return "{'result':'1','info':'门户实例停用发布失败!'}";
			}
		} catch (Exception e) {
			return "{'result':'2','info':'接口异常!'}";
		}
		return "{'result':'0','info':'门户实例停用发布成功!'}";
	}

}
